{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf1a676a",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Parent:\n",
    "    def __init__(self, name):\n",
    "        self.name = name\n",
    "\n",
    "class Child(Parent):\n",
    "    def __init__(self, name, age):\n",
    "        super().__init__(name)\n",
    "        self.age = age\n",
    "\n",
    "parent_obj = Parent(\"John\")\n",
    "\n",
    "child_obj = type(Child) parent_obj\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "baa5ae1e",
   "metadata": {},
   "outputs": [],
   "source": [
    "run(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3484905f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import threading\n",
    "class SingleInstance:\n",
    "    instance = None\n",
    "    lock = threading.Lock()\n",
    "    def __new__(cls):\n",
    "        with cls.lock:\n",
    "            if cls.instance is None:\n",
    "                cls.instance = super().__new__(cls)\n",
    "            return cls.instance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4e7bce9d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def decorater(func):\n",
    "    def inner(*args,**kwargs):\n",
    "        print(*args,**kwargs)\n",
    "        return func(*args,**kwargs)\n",
    "    return inner"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aea835fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "@decorater\n",
    "def run(a,b):\n",
    "    return a+b\n",
    "run(3,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cedfcd02-3ed7-4a26-8ec0-511eb04a4a37",
   "metadata": {},
   "outputs": [],
   "source": [
    "import threading\n",
    "import time\n",
    "class Test:\n",
    "    # _instance_lock = threading.Lock()\n",
    "    _instance = None\n",
    "    def __new__(cls,*args,**kwargs):\n",
    "        if cls._instance is None:\n",
    "            print(222)\n",
    "            cls._instance = super().__new__(cls)\n",
    "        return cls._instance\n",
    "    def __init__(self,a,b,c):\n",
    "        self.a = a\n",
    "        # time.sleep(3)\n",
    "        print(111)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd6a220c-8953-4856-9843-737a1d57caed",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Test:\n",
    "    def __new__(cls,*args,**kwargs):\n",
    "        return super().__new__(cls)\n",
    "    def __init__(self,a,b,c):\n",
    "        print(111)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "702bbda6-8ced-45c6-a3e9-eba6d0b3e887",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(Test(1,2,3))\n",
    "print(Test(1,2,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f67bfc9c-1d74-4a09-90fc-b608e9014876",
   "metadata": {},
   "outputs": [],
   "source": [
    "import threading\n",
    "import time\n",
    "class Singleton(object):\n",
    "    def __init__(self):\n",
    "        print(222)\n",
    "        time.sleep(1)\n",
    "    @classmethod\n",
    "    def instance(cls, *args, **kwargs):\n",
    "        #with Singleton._instance_lock: # 加锁\n",
    "        if not hasattr(Singleton, '_instance'):\n",
    "            print(111)\n",
    "            Singleton._instance = Singleton(*args, **kwargs)\n",
    "        return Singleton._instance\n",
    "def test(arg):\n",
    "    obj1 = Singleton.instance()\n",
    "    print(333)\n",
    "    # obj2 = Singleton.instance()\n",
    "    print(obj1)\n",
    "test(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "184f939f-4869-4434-8a21-2ab01bc12a32",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "for i in range(100):\n",
    "    t = threading.Thread(target=test,args=[i,])\n",
    "    t.start()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.5 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.16"
  },
  "vscode": {
   "interpreter": {
    "hash": "3d597f4c481aa0f25dceb95d2a0067e73c0966dcbd003d741d821a7208527ecf"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
